####################################################################### # Returns a random variable based on a given distribution using JAVA # ####################################################################### # Get the current working directory, and set the names for temporary files to be used CC "AdoScript" GET_CWD SET sCWD:(cwd) SET sJavaFileTemp:(sCWD + "\\tools\\GenerateRandomValue.class") SET sTempResultsFile:(sCWD + "\\tools\\RandValTemp.txt") # Copy the Java program to a temporary location to be run. If there is an error in copying, inform the user CC "AdoScript" FILE_COPY from:("db:\\GenerateRandomValue.class") to:(sJavaFileTemp) IF (ecode != 0) { CC "AdoScript" ERRORBOX "Error copying file, you might need to start modelling toolkit with Windows administrator privileges" } # Load the string that defines the distribution, which is stored in the attribute "Distribution Type" CC "Core" GET_ATTR_VAL objid:(nObjID) attrname:"Distribution Type" SET sDistTypeAttrString:(val) # Determine which distribution type it is by parsing the string. For the moment, assume only one type is present. SET sDistType:(copy(sDistTypeAttrString, 0, search(sDistTypeAttrString,"(", 0))) IF (sDistType = "Normal") { # Parse the string to find the parameters SET fMu:(VAL replall(copy(sDistTypeAttrString, search(sDistTypeAttrString,"(", 0) + 1, search(sDistTypeAttrString,";", 0) - 1), ",", ".")) SET fSigma:(VAL replall(copy(sDistTypeAttrString, search(sDistTypeAttrString,";", 0) + 2, search(sDistTypeAttrString,")", 0) - 1), ",", ".")) # Run the Java program, passing the distribution parameters and the location of a temporary file where the random value is written SET sJavaTempResultsFile:(replall(sTempResultsFile, "\\", "/")) SYSTEM ("cmd /c java -cp \"" + sCWD + "\\tools\" GenerateRandomValue " + "\"" + sJavaTempResultsFile + "\"" + " " + sDistType + " " + (STR fMu) + " " + (STR fSigma)) } ELSIF (sDistType = "Uniform") { # Parse the string to find the parameters SET fLowerUniform:(VAL replall(copy(sDistTypeAttrString, search(sDistTypeAttrString,"(", 0) + 1, search(sDistTypeAttrString,";", 0) - 1), ",", ".")) SET fUpperUniform:(VAL replall(copy(sDistTypeAttrString, search(sDistTypeAttrString,";", 0) + 2, search(sDistTypeAttrString,")", 0) - 1), ",", ".")) IF (fUpperUniform = fLowerUniform) { CC "AdoScript" ERRORBOX ("Error: Uniform distribution undefined on zero length interval (Lower bound = Upper bound)") EXIT } ELSIF (fUpperUniform < fLowerUniform) { CC "AdoScript" ERRORBOX ("Error: Upper bound < Lower bound") EXIT } ELSE { # Run the Java program, passing the distribution parameters and the location of a temporary file where the random value is written SET sJavaTempResultsFile:(replall(sTempResultsFile, "\\", "/")) SYSTEM ("cmd /c java -cp \"" + sCWD + "\\tools\" GenerateRandomValue " + "\"" + sJavaTempResultsFile + "\"" + " " + sDistType + " " + (STR fUpperUniform) + " " + (STR fLowerUniform)) } } ELSIF (sDistType = "Exponential") { # Parse the string to find the parameters SET fLambda:(VAL replall(copy(sDistTypeAttrString, search(sDistTypeAttrString,"(", 0) + 1, search(sDistTypeAttrString,")", 0) - 1), ",", ".")) IF (fLambda = 0) { CC "AdoScript" ERRORBOX ("Error: Rate cannot be zero for exponential distribution") EXIT } ELSE { # Run the Java program, passing the distribution parameters and the location of a temporary file where the random value is written SET sJavaTempResultsFile:(replall(sTempResultsFile, "\\", "/")) SYSTEM ("cmd /c java -cp \"" + sCWD + "\\tools\" GenerateRandomValue " + "\"" + sJavaTempResultsFile + "\"" + " " + sDistType + " " + (STR fLambda)) } } ELSIF (sDistType = "Triangle") { # Parse parameters when the dialog box has the triangle option SET sDistTypeAttrString:(replall(sDistTypeAttrString, "Triangle", "")) SET sDistTypeAttrString:(replall(sDistTypeAttrString, "(", "")) SET sDistTypeAttrString:(replall(sDistTypeAttrString, " ", "")) SET sDistTypeAttrString:(replall(sDistTypeAttrString, ")", "")) SET nFirstPos:(search(sDistTypeAttrString,";", 0)) SET nSecondPos:(search(sDistTypeAttrString,";", nFirstPos + 1)) SET fLowerTriangle:(VAL copy(sDistTypeAttrString, 0, nFirstPos)) SET fMediumTriangle:(VAL copy(sDistTypeAttrString, nFirstPos + 1, nSecondPos - 1)) SET fUpperTriangle:(VAL copy(sDistTypeAttrString, nSecondPos + 1, LEN (sDistTypeAttrString))) IF ((fUpperTriangle < fLowerTriangle) OR (fUpperTriangle < fMediumTriangle) OR (fMediumTriangle < fLowerTriangle)) { CC "AdoScript" ERRORBOX ("Error: Parameters need to be Lower < Medium < Upper") EXIT } ELSE { # Run the Java program, passing the distribution parameters and the location of a temporary file where the random value is written SET sJavaTempResultsFile:(replall(sTempResultsFile, "\\", "/")) SYSTEM ("cmd /c java -cp \"" + sCWD + "\\tools\" GenerateRandomValue " + "\"" + sJavaTempResultsFile + "\"" + " " + sDistType + " " + (STR fUpperTriangle) + " " + (STR fMediumTriangle) + " " + (STR fLowerTriangle)) } } ELSIF (sDistType = "Discrete") { CC "AdoScript" INFOBOX "Discrete distribution not presently supported using Java" } ELSE { CC "AdoScript" ERRORBOX ("Error: Distribution type not found/recognised") EXIT } # The Java program will have saved a random value in the temporary file. We now read in this value from the file. CC "AdoScript" FREAD file:(sTempResultsFile) SET sRandVal:(VAL text) # We write the random value to the attribute "Random Value". Note: Since we have to allow for the case of distribution of type "Discrete" # this attribute is a string. CC "Core" SET_ATTR_VAL objid:(nObjID) attrname:"Random Value" val:(sRandVal) # Clean up the temporary files that were created. CC "AdoScript" DELETE_FILES (sTempResultsFile) CC "AdoScript" DELETE_FILES (sJavaFileTemp)